package com.mlamp.demo.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.ImmutableMap;
import com.mlamp.demo.data.mode.TbGlUser;
import com.mlamp.demo.service.IndexService;
import com.mlamp.demo.web.controller.base.BaseController;

@Controller(value="indexController")
@RequestMapping(value = "/index")
public class IndexController extends BaseController{
	
	@Resource
	IndexService indexService;
	
	
	@RequestMapping(value="/json",method=RequestMethod.GET)
	@ResponseBody
	public Object json(HttpServletRequest request)
	{

		Map<String, String> result=new HashMap<>();
		result.put("ok", "1");
		return result;
	}
	
	@RequestMapping(value="/findPage",method=RequestMethod.GET)
	@ResponseBody
	public Object findPage(HttpServletRequest request)
	{
		String username=request.getParameter("username");
		Map<String,Object> params=ImmutableMap.of("username",(Object)username);
		PageInfo<TbGlUser> pages=indexService.findPage(params);
		
		return pages;
	}
	
	
	@RequestMapping(value="/findOne",method=RequestMethod.GET)
	@ResponseBody
	public Object findOne(HttpServletRequest request)
	{
		String id=request.getParameter("id");
		TbGlUser pages=indexService.findOne(id);	
		return pages;
	}
	
	@RequestMapping(value="/findByParams",method=RequestMethod.GET)
	@ResponseBody
	public Object findByParams(HttpServletRequest request)
	{
		String username=request.getParameter("username");
		Map<String,Object> params=ImmutableMap.of("username",(Object)username);
		List<Map<String, Object>> pages=indexService.findByParams(params);	
		return pages;
	}
	
	
	/**
	 * 
	 * {
"sfzh": "642101197711091136",
"username": "ld",
"ywxm": "",
"password": "111111",
"zzjgdm": "640000000000",
"zzjgmc": "宁夏回族自治区",
"email": "",
"jinghao": "",
"zhiwu": "",
"phone": "",
"description": "",
"deleted": 0,
"lrsj": 1514516522000,
"jingzhong": "",
"yxqx": "2017-12-31",
"dwmc": "",
"top": null
}
	 * @param record
	 * @return
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@ResponseBody
	public Object save(@RequestBody TbGlUser record)
	{
		Map<String, String> result=new HashMap<>();
		result.put("ok", "1");
		indexService.save(record);
		return result;
	}
	
	
	/**
	 * 
	 * {
"sfzh": "642101197711091136",
"username": "ld",
"ywxm": "",
"password": "111111",
"zzjgdm": "640000000000",
"zzjgmc": "宁夏回族自治区11111",
"email": "",
"jinghao": "",
"zhiwu": "",
"phone": "",
"description": "",
"deleted": 0,
"lrsj": 1514516522000,
"jingzhong": "",
"yxqx": "2017-12-31",
"dwmc": "",
"top": null
}
	 * @param record
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Object update(@RequestBody TbGlUser record)
	{
		Map<String, String> result=new HashMap<>();
		result.put("ok", "1");
		indexService.update(record);
		return result;
	}
	
	
	@RequestMapping(value="/go",method=RequestMethod.GET)
	public ModelAndView go(HttpServletRequest request)
	{
		ModelAndView mv=new ModelAndView("index/go");
		return mv;
	}
	
	
	/**
	 * curl  -F "file=@test2.bpmn" "http://localhost:8080/demo/index/fkUpdateFile"
	 * @param params
	 * @param req
	 * @param res
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/fkUpdateFile")
	@ResponseBody
    public Map<String, Object> fkUpdateFile(Map<String,Object> params,HttpServletRequest req,HttpServletResponse res,HttpSession session) throws Exception {		
		Map<String, Object> fileInfo=upload(req);
	
		return fileInfo;
    }

}
